package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.BleBeacon;
import com.tbit.uqbike.service.business.BleBeaconService;
import com.tbit.uqbike.webmanager.dao.core.BleBeaconDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: chen
 * @Description: 道钉
 * @Date: 创建于 9:55 2020/5/15
 */
@Service
public class BleBeaconServiceImpl implements BleBeaconService {
    @Autowired
    private BleBeaconDao bleBeaconDao;

    @Override
    public List<BleBeacon> queryPage(Integer state, Integer parkPointId, String machineNO,
                                     Integer pageNO, Integer rowCount, Integer accountId) {
        Map<String, Object> params = new HashMap<>(16);
        params.put("parkPointId", parkPointId);
        params.put("state", state);
        params.put("machineNO", machineNO);
        params.put("pageNO", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);
        params.put("accountId", accountId);
        return bleBeaconDao.queryPage(params);
    }

    @Override
    public Integer queryCount(Integer state, Integer parkPointId, String machineNO, Integer accountId) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("parkPointId", parkPointId);
        params.put("state", state);
        params.put("machineNO", machineNO);
        params.put("accountId", accountId);
        return bleBeaconDao.queryCount(params);
    }

    @Override
    public void insert(BleBeacon bleBeacon) {
        BleBeacon bleBeaconDB = bleBeaconDao.getByNO(bleBeacon.getMachineNO(), bleBeacon.getAccountId());
        if (bleBeaconDB != null) {
            throw new BaseException("ver.bleBeacon.exist");
        }
        bleBeacon.setAddTime(new Date());
        bleBeacon.setState(0);
//    checkMachineNO(bleBeacon);
        bleBeaconDao.insert(bleBeacon);
    }

    @Override
    public void del(Integer bleBeaconId) {
        bleBeaconDao.del(bleBeaconId);
    }

    @Override
    public List<String> addBatch(Integer parkPointId, List<String> machineNOs, Integer accountId) {
        /**检查是否存在*/
        List<String> exitMachineNOs = bleBeaconDao.getByMachineNOs(machineNOs, accountId);
        if (exitMachineNOs.size() > 0) {
            return exitMachineNOs;
        }
        List<BleBeacon> list = new LinkedList<>();
        for (String machineNO : machineNOs) {
            BleBeacon bleBeacon = new BleBeacon();
            bleBeacon.setState(0);
            bleBeacon.setAddTime(new Date());
            bleBeacon.setMachineNO(machineNO);
            bleBeacon.setParkPointId(parkPointId);
            bleBeacon.setAccountId(accountId);
//      checkMachineNO(bleBeacon);
            list.add(bleBeacon);
        }
        bleBeaconDao.addBatch(list);
        return null;
    }

    private void checkMachineNO(BleBeacon bleBeacon) {
        String machineNO = bleBeacon.getMachineNO();
        if (Integer.parseInt(machineNO) >= 122000000 && Integer.parseInt(machineNO) <= 122199999) {
            bleBeacon.setMachineNO(machineNO + "000");
        }
    }
}
